<?php

class IndexAction extends PublicAction
{

    public function index()
    {
        $v = D()->query("select VERSION() as version");
        $info = [
            '运行环境' => PHP_OS . ' ' . $_SERVER["SERVER_SOFTWARE"],
            'PHP运行方式' => php_sapi_name(),
            'MYSQL版本' => $v[0]['version'],
            '上传附件限制' => ini_get('upload_max_filesize'),
            '执行时间限制' => ini_get('max_execution_time') . '秒',
            '剩余空间' => round((@disk_free_space(".") / (1024 * 1024)), 2) . 'M',
        ];

        $this->assign('server_info', $info);

        $models = array();
        foreach ((array)$this->Model as $val) {
            if ($val['type'] == 1 && $val['status'] == 1) {
                $models[] = $val;
                $model_db = M($val['tablename']);
                $mdata[$val['tablename']] = $model_db->count();
            }
        }

        $user_db = D('Admin');
        $userinfos = $user_db->find($_SESSION['adminid']);

        $this->assign('models', $models);
        $this->assign('mdata', $mdata);

        $userinfo = array(
            'username' => $userinfos['username'],
            'login_time' => toDate($userinfos['last_login_time']),
            'last_ip' => $userinfos['last_ip'],
            'login_count' => $userinfos['login_count'] . '次',
        );

        $this->assign('userinfo', $userinfo);
        $this->assign($this->Config);

        $this->display();
    }


    function getnav($menuid)
    {
        $nav = array();

        if ($this->Menu) {

            $rules = M('AuthGroup')->where('id='.$_SESSION['role_id'])->getField('rules');
            $accessList = M('AuthRule')->where(array('id'=>array('in', $rules)))->getField('name', true);

            foreach ($this->Menu as $key => $module) {

                if ($module['parentid'] != $menuid || $module['status'] == 0)
                    continue;

                if (in_array($module['node'], $accessList) || !empty($_SESSION[C('ADMIN_AUTH_KEY')])) {

                    $nav[$key] = $module;

                    $array = array('menuid' => $nav[$key]['parentid']);

                    if (empty($menuid) && empty($isnav))
                        $array = array();
                    $c = array();
                    parse_str($nav[$key]['data'], $c);
                    $nav[$key]['data'] = $c + $array;

                }
            }
        }

        return $nav;
    }


    public function profile()
    {
        $adminModel = D('Admin');
        if (IS_POST) {
            $password = $_POST['password'];
            $repassword = $_POST['repassword'];
            $_POST['id'] = session('adminid');

            //为空 不修改密码
            if (empty($password) && empty($repassword)) {
                unset($_POST['password']);
                unset($_POST['repassword']);
            }

            if (!empty($password) || !empty($repassword)) {
                if ($password != $repassword) {
                    $this->error('两次输入密码不一致！');
                }
                $_POST['salt'] = genRandomString(6);
                $_POST['password'] = md5($password . md5($_POST['salt']));
            }

            //根据表单提交的POST数据创建数据对象
            if ($adminModel->create()) {
                if ($adminModel->save()) {
                    $this->success('编辑成功！');
                }
            } else {
                $this->error($adminModel->getError());
            }
        }
        $user = $adminModel->find(session('adminid'));
        $this->assign('user', $user);
        $this->display();
    }
}